import './interfaces';
import send_msg from '@feidao/service/send-msg';

export default function update(action_id: string, session_id: string, spaceid: string, systemid: string) {
	const argjson = [] as IArgJsonUpdate[];

	function prepare<T extends IBaseTable>(tname: string, data: T[]) {
		argjson.push(...data.map((it) => {
			return {
				criteria: {
					_id: it._id
				},
				opflag: 8,
				rowdata: it,
				tname
			};
		}));
	}

	function exec() {
		const msg = {
			argjson,
			spaceid,
			systemid
		};
		return send_msg(action_id, session_id, spaceid)('dbacc.save.intranet', msg) as Promise<{ code: number; result: any; }>;
	}

	return {
		exec,
		prepare
	};
}
